import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/input_item.dart';
import 'package:xinxuan/common/input_item_sms_code.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/bank_card_controller.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/color_util.dart';

class AddEditBankCardPage extends StatelessWidget {

  final BankCardController bankCardController = Get.find();

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> argumentsMap = Get.arguments;
    String title = argumentsMap['title'];
    int type = argumentsMap['type'];
    int bankType = argumentsMap['bankType'];
    if (argumentsMap['data'] != null) {
      Map<String, dynamic> mapData = argumentsMap['data'];
      bankCardController.ininEditBank(mapData, bankType);
    } else {
      bankCardController.ininAddBank(bankType);
    }
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: title,
        backgroundColor: pageBgColor,
        actions: [
          type == 0 ? InkWellUtil.shakeProofInkWell(Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Center(
              child: Text(
                '完成',
                style: TextStyle(
                  color: mainTextColor,
                  fontSize: 14.sp
                ),
              ),
            ),
          ), () {
            bankCardController.addBankCard();
          },
          hoverColor: Colors.transparent,
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent) : Container()
          
          // InkWellUtil.shakeProofInkWell(Container(
          //   padding: EdgeInsets.symmetric(horizontal: 16.w),
          //   child: Center(
          //     child: Text(
          //       '删除',
          //       style: TextStyle(
          //         color: mainTextColor,
          //         fontSize: 14.sp
          //       ),
          //     ),
          //   ),
          // ), () {
          //   bankCardController.delBankCard();
          // },
          // hoverColor: Colors.transparent,
          // splashColor: Colors.transparent,
          // highlightColor: Colors.transparent)
        ]
      ),
      body: GestureDetector(
        onTap: () {
          // if (MediaQuery.of(context).viewInsets.bottom==0) {
          //   FocusScope.of(context).requestFocus(FocusNode());
          // }
        },
        child: GetBuilder<BankCardController>(
          builder: (controller) {
            return ScrollConfigurationCommon(child: ListView(
              children: [
                InputItem(
                  title: '账号名', 
                  hintText: '请填写账户名', 
                  defaultValue: '${bankCardController.accountName.value}', 
                  maxLength: 10,
                  onChenge: (String value) {
                    bankCardController.accountName.value = value;
                  }
                ),
                InputItem(
                  title: '账号', 
                  hintText: bankType == 2 ? '请填写银行卡账号' : '请填写银行或支付宝账号', 
                  defaultValue: '${bankCardController.cardNumber.value}', 
                  keyBoardType: TextInputType.number, 
                  maxLength: 20,
                  onChenge: (String value) {
                    bankCardController.cardNumber.value = value;
                  }
                ),
                _bankListItem(),
                Obx(() => controller.bankId.value != 1 && controller.bankId.value != 2 ? InputItem(
                  title: '开户支行', 
                  hintText: '请填写开户支行', 
                  defaultValue: '${bankCardController.subbranch}', 
                  maxLength: 32,
                  onChenge: (String value) {
                    bankCardController.subbranch = value;
                  }
                ) : Container()),
                InputItem(
                  title: '身份证号', 
                  hintText: '请输入身份证号码', 
                  defaultValue: '${bankCardController.identityNumber.value}', 
                  maxLength: 32,
                  keyBoardType: TextInputType.number, 
                  onChenge: (String value) {
                    bankCardController.identityNumber.value = value;
                  }
                ),
                Obx(() => InputItemSmsCode(
                  value: bankCardController.verificationCode.value,
                  duration: bankCardController.duration.value,
                  onChenge: (String value) {
                    bankCardController.verificationCode.value = value;
                  }, 
                  onTap: () {
                    if (bankCardController.duration.value >= 60) {
                      bankCardController.getSmsCode();
                    }
                  }
                )),
                // _cardPurposeItem(type: type),
                // ToggleDefaultItem(
                //   title: '设为默认卡片', 
                //   value: bankCardController.isDefault.value == 1, 
                //   showBorder: false,
                //   onToggle: (bool value) {
                //     bankCardController.isDefault.value = value ? 1 : 0;
                //     bankCardController.update();
                //   }
                // ),
                type == 0 ? Container() : Container(
                  padding: EdgeInsets.symmetric(horizontal: 25.w),
                  margin: EdgeInsets.only(top: 39.h),
                  child: ButtonUtil(
                    text: '保存', 
                    fontSize: 16.sp, 
                    borderRadius: 30.w, 
                    height: 36.h, 
                    onTop: () {
                      bankCardController.editBankCard();
                    }
                  ),
                )
              ],
            ));
          },
        )
      ),
    );
  }

  /// 银行卡下拉项
  Widget _bankListItem() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        height: 54.h,
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: .5,
              color: lineColor
            )
          )
        ),
        child: Row(
          children: [
            Container(
              width: 84.w,
              child: Text(
                '所属银行',
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 16.sp
                ),
              ),
            ),
            Expanded(
              child: InkWellUtil.shakeProofInkWell(Row(
                children: [
                  Expanded(child: Container(
                    child: Text(
                      bankCardController.selectedBankItem.value.title == null ? '请选择开户行' : '${bankCardController.selectedBankItem.value.title}',
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 16.sp
                      ),
                    ),
                  )),
                  Image.asset(
                    'assets/icons/icon_arrow_right.png',
                    width: 15.w,
                    height: 15.w,
                  )
                ],
              ), () {
                Get.bottomSheet(Container(
                  height: 300.h,
                  decoration: BoxDecoration(
                    color: bgWhiteColor,
                    borderRadius: BorderRadius.vertical(top: Radius.circular(5.w))
                  ),
                  child: Column(
                    children: [
                      Container(
                        height: 54.h,
                        padding: EdgeInsets.symmetric(horizontal: 16.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            InkWellUtil.shakeProofInkWell(Text(
                              '取消',
                              style: TextStyle(
                                color: textSubtitleColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              Get.back();
                            }),
                            InkWellUtil.shakeProofInkWell(Text(
                              '确认',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              bankCardController.determineBank();
                              Get.back();
                            })
                          ],
                        ),
                      ),
                      Expanded(
                        child: ScrollConfigurationCommon(child: CupertinoPicker(
                          scrollController: bankCardController.scrollController.value,
                          diameterRatio: 5.w,
                          itemExtent: 54.h, 
                          onSelectedItemChanged: (int index) {
                            bankCardController.changeBankIndex(index);
                          }, 
                          children: bankCardController.systemBankArr.map((item) {
                            return Container(
                              height: 54.h,
                              alignment: Alignment.center,
                              child: Text(
                                '${item.title}',
                                style: TextStyle(
                                  color: textBlackColor,
                                  fontSize: 16.sp
                                ),
                              ),
                            );
                          }).toList()
                        )),
                      )
                    ],
                  ),
                ));
              }),
            )
          ],
        ),
      ),
    );
  }

  /// 卡片用途
  // Widget _cardPurposeItem({int type = 0}) {
  //   return Container(
  //     padding: EdgeInsets.symmetric(horizontal: 12.w),
  //     color: bgWhiteColor,
  //     child: Container(
  //       padding: EdgeInsets.symmetric(horizontal: 4.w),
  //       height: 54.h,
  //       decoration: BoxDecoration(
  //         border: Border(
  //           bottom: BorderSide(
  //             width: .5,
  //             color: lineColor
  //           )
  //         )
  //       ),
  //       child: Row(
  //         children: [
  //           Container(
  //             width: 84.w,
  //             child: Text(
  //               '卡片用途',
  //               style: TextStyle(
  //                 color: textBlackColor,
  //                 fontSize: 16.sp
  //               ),
  //             ),
  //           ),
  //           Expanded(
  //             child: InkWellUtil.shakeProofInkWell(Row(
  //               children: [
  //                 Expanded(child: Container(
  //                   child: Text(
  //                     bankCardController.type.value == 1 ? '抢购' : '提现',
  //                     style: TextStyle(
  //                       color: textBlackColor,
  //                       fontSize: 16.sp
  //                     ),
  //                   ),
  //                 )),
  //                 type == 1 ? Container() : Image.asset(
  //                   'assets/icons/icon_arrow_right.png',
  //                   width: 15.w,
  //                   height: 15.w,
  //                 )
  //               ],
  //             ), () {
  //               if (type == 1) {
  //                 return;
  //               }
  //               Get.bottomSheet(Container(
  //                 height: 300.h,
  //                 decoration: BoxDecoration(
  //                   color: bgWhiteColor,
  //                   borderRadius: BorderRadius.vertical(top: Radius.circular(5.w))
  //                 ),
  //                 child: Column(
  //                   children: [
  //                     Container(
  //                       height: 54.h,
  //                       padding: EdgeInsets.symmetric(horizontal: 16.w),
  //                       child: Row(
  //                         mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //                         crossAxisAlignment: CrossAxisAlignment.center,
  //                         children: [
  //                           InkWellUtil.shakeProofInkWell(Text(
  //                             '取消',
  //                             style: TextStyle(
  //                               color: textSubtitleColor,
  //                               fontSize: 16.sp
  //                             ),
  //                           ), () {
  //                             Get.back();
  //                           }),
  //                           InkWellUtil.shakeProofInkWell(Text(
  //                             '确认',
  //                             style: TextStyle(
  //                               color: mainTextColor,
  //                               fontSize: 16.sp
  //                             ),
  //                           ), () {
  //                             bankCardController.determinePurpose();
  //                             Get.back();
  //                           })
  //                         ],
  //                       ),
  //                     ),
  //                     Expanded(
  //                       child: ScrollConfigurationCommon(child: CupertinoPicker(
  //                         scrollController: bankCardController.cardPurposeScrollController.value,
  //                         diameterRatio: 5.w,
  //                         itemExtent: 54.h, 
  //                         onSelectedItemChanged: (int index) {
  //                           bankCardController.changePurposeIndex(index);
  //                         }, 
  //                         children: bankCardController.availableTypeList.map((item) {
  //                           return Container(
  //                             height: 54.h,
  //                             alignment: Alignment.center,
  //                             child: Text(
  //                               '${item['label']}',
  //                               style: TextStyle(
  //                                 color: textBlackColor,
  //                                 fontSize: 16.sp
  //                               ),
  //                             ),
  //                           );
  //                         }).toList(),
  //                       )),
  //                     )
  //                   ],
  //                 ),
  //               ));
  //             }),
  //           )
  //         ],
  //       ),
  //     ),
  //   );
  // }
  
}